<?php
/**
 * 产品在仓库库存模型
 */
class Application_Model_ProductWarehouseStock extends Model_Base {

	public function __construct() {
		$this->db = $this->db();
	}

	/**
	 * 获取一条产品库存记录
	 * @param  [type] $goodsSn   [description]
	 * @param  [type] $warehouse [description]
	 * @param  string $fields    [description]
	 * @return [type]            [description]
	 */
	public function getOne($goodsSn, $warehouse, $fields = '[*]') {
		$select = $this->db->select($fields)->from('p_product_warehouse_stock')->where('goods_sn=:goodsSn')->where('warehouse_code=:warehouse');
		return $this->db->fetchRow($select, ['goodsSn' => $goodsSn, 'warehouse' => $warehouse]);
	}

	/**
	 * 获取产品的可用库存
	 * @param  [type] $goodsSn   [description]
	 * @param  [type] $warehouse [description]
	 * @return [type]            [description]
	 */
	public function getAvailableStock($goodsSn, $warehouse) {
		$stockInfo = $this->getOne($goodsSn, $warehouse, ['base_stock']);
		if (empty($stockInfo)) {
			return 0;
		}

		return $stockInfo['base_stock'];
	}
}